package com.wgw.first.home.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.wgw.first.R;
import com.wgw.first.dao.HomeBean;
import com.wgw.first.utils.Constants;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerClickListener;
import com.youth.banner.listener.OnLoadImageListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Admin
 * @version $Rev$
 * @des ${TODO}
 * @updateAuthor $Author$
 * @updateDes ${TODO}
 */
public class HomeFragmentAdapter extends RecyclerView.Adapter{


    private  LayoutInflater mLayoutInflater;
    private  HomeBean homeBean;

    private int currentType = BANNER;

    private static final int BANNER = 0;
    private static final int NEWBOOK = 1;
    private static final int EXCLUSIVE = 2;
    private static final int GOODBOOK = 3;
    private Context mContext;

    public HomeFragmentAdapter(Context mcontext,HomeBean homeBean){
        this.mContext = mcontext;
        this.homeBean = homeBean;
        mLayoutInflater = LayoutInflater.from(mcontext);

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == BANNER){
            return new BannerViewHolder(mContext,mLayoutInflater.inflate(R.layout.banner_viewpager,null));
        }else if(viewType == NEWBOOK){
            return new NEWBOOKViewHolder(mContext,mLayoutInflater.inflate(R.layout.newbook,null));
        }else if(viewType == EXCLUSIVE){
            return new ExclusiveViewHolder(mContext,mLayoutInflater.inflate(R.layout.exclusive,null));
        }else if(viewType == GOODBOOK){
            return new ListGoodBookViewHolder(mContext,mLayoutInflater.inflate(R.layout.goodbook,null));
        }


        return null;


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(getItemViewType(position)==BANNER){
            BannerViewHolder bannerViewHolder = (BannerViewHolder) holder;
            bannerViewHolder.setData(homeBean.getBanner_info());
        }else if(getItemViewType(position)==NEWBOOK){
            NEWBOOKViewHolder newbookViewHolder = (NEWBOOKViewHolder)holder;
            newbookViewHolder.setData();
        }else if(getItemViewType(position)==EXCLUSIVE){
            ExclusiveViewHolder exclusiveViewHolder = (ExclusiveViewHolder)holder;
            exclusiveViewHolder.setData();
        }else if(getItemViewType(position)==GOODBOOK){
            ListGoodBookViewHolder listgoodbookViewHolder = (ListGoodBookViewHolder)holder;
            listgoodbookViewHolder.setData();
        }

    }

    @Override
    public int getItemCount() {
        return 4;
    }
    public int getItemViewType(int position) {
        switch (position) {
            case BANNER:
                currentType = BANNER;
                break;
            case NEWBOOK:
                currentType = NEWBOOK;
                break;
            case EXCLUSIVE:
                currentType = EXCLUSIVE;
                break;
            case GOODBOOK:
                currentType = GOODBOOK;
                break;
        }
        return currentType;
    }

    private class ListGoodBookViewHolder extends RecyclerView.ViewHolder {

        private RecyclerView rv_list_goodbook;

        public ListGoodBookViewHolder(Context mcontext, View itemview) {
            super(itemview);
            rv_list_goodbook = (RecyclerView)itemview.findViewById(R.id.rv_list_goodbook);
        }

        public void setData() {
            ListGoodBookAdapter listGoodBookAdapter = new ListGoodBookAdapter(mContext,homeBean.getGoodbook_info());
            rv_list_goodbook.setAdapter(listGoodBookAdapter);
            rv_list_goodbook.setLayoutManager(new GridLayoutManager(mContext,2));
        }
    }

    private class ExclusiveViewHolder extends RecyclerView.ViewHolder {

        private final Context mcontext;
        private final RecyclerView rv_exclusive_book;

        public ExclusiveViewHolder(Context mcontext, View itemview) {
            super(itemview);
            this.mcontext = mcontext;
            rv_exclusive_book = (RecyclerView) itemview.findViewById(R.id.rv_exclusive_book);
        }

        public void setData() {
            //设置独家推荐适配器
            ExclusiveBookAdapter exclusiveAdapter = new ExclusiveBookAdapter(mcontext,homeBean.getExclusive_info());
            rv_exclusive_book.setAdapter(exclusiveAdapter);
            rv_exclusive_book.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        }
    }

    private class NEWBOOKViewHolder extends RecyclerView.ViewHolder {

        private Context mcontext;
        private RecyclerView rv_newbook;
        public NEWBOOKViewHolder(Context mcontext, View itemview) {
            super(itemview);
            this.mcontext = mcontext;
            rv_newbook = (RecyclerView)itemview.findViewById(R.id.rv_newbook);
        }

        public void setData() {

            //设置新书推荐
            NewBookAdapter newBookAdapter = new NewBookAdapter(mContext,homeBean.getNewbook_info());
            rv_newbook.setAdapter(newBookAdapter);
            rv_newbook.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));

        }
    }

    private class BannerViewHolder extends RecyclerView.ViewHolder {

        private Context mcontext;
        private Banner banner;

        //        public BannerViewHolder(Context mcontext, View itemView, ResultBean resultbean) {
        public BannerViewHolder(Context mcontext, View itemView) {
            super(itemView);
            this.mcontext = mcontext;
            this.banner = (Banner)itemView.findViewById(R.id.banner);


        }
        //        public void setData(final list<ResultBean.BannerInfoBean banner_info){
        public void setData(List<HomeBean.BannerInfoBean> banner_info){
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            banner.setBannerAnimation(Transformer.Accordion);
            List<String> imageUris = new ArrayList<>();
            for(int i = 0;i<homeBean.getBanner_info().size();i++){
                imageUris.add(homeBean.getBanner_info().get(i).getBanner_imgurl());
            }

            banner.setImages(imageUris, new OnLoadImageListener() {
                @Override
                public void OnLoadImage(ImageView view, Object url) {
                    /**
                     * 这里你可以根据框架灵活设置
                     */
                    Glide.with(mContext)
                            .load(Constants.BASE + Constants.IMG + url)
                            .into(view);
                }
            });
            //设置点击事件
            banner.setOnBannerClickListener(new OnBannerClickListener() {
                @Override
                public void OnBannerClick(int position) {
                    Toast.makeText(mcontext, "positon:"+position, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }


}
